<?php
/**
 * Front Controller plug in to set up the view with the Places view helper
 * path and some useful request variables.
 *
 */
class Wmp_Controller_Plugin_ViewSetup extends Zend_Controller_Plugin_Abstract
{    
    /**
     * @var Zend_View
     */
    protected $_view;    
    protected $_layoutName;
        
    public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
    {
       	$config = Zend_Registry::get('config');
    	$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
        $viewRenderer->init();
        $viewRenderer->setViewSuffix($config->views->suffix);        
        $view = $viewRenderer->view;
        $this->_view = $view;
        
        //Zend_Debug::dump($view); exit(1);
        
        // set up common variables for the view
        $view->module = $request->getModuleName();
        $view->controller = $request->getControllerName();
        $view->action = $request->getActionName();
               
        $moduleTable = new Wmp_Model_ModulesTable();
        $moduleTable = $moduleTable->find($request->getModuleName())->current();
    	$registredTemplate = $moduleTable->template;
       	if(!$registredTemplate) $registredTemplate = $config->defaultTemplate;
    	
    	$actionParams = new Wmp_Model_ActionparamsTable();
    	$actionParams = $actionParams->getRow($view->module , $view->controller , $view->action);        

    	$layoutName = $actionParams->layout;
    	if(!$layoutName) $layoutName = $config->layouts->name;
    	//saving in the registry
    	Zend_Registry::set('template', $registredTemplate);
    	Zend_Registry::set('layoutName', $layoutName);
		Zend_Registry::set('module', $view->module);
		Zend_Registry::set('controller', $view->controller); 
		Zend_Registry::set('action', $view->action);
		
		
		// set up doctype for any view helpers that use it
        $view->doctype('XHTML1_STRICT');
         
		/*--------------------------------------------------------------------------
		 * Définition des chemins de view/helper:
		 * les views helpers peuvent être définies dans deux endroits:
		 * -Soit dans le modules/views/helpers/ (en priorité)
		 * -Soit dans library/Wmp/View/Helper
		 */
        // add helper path to Wmp/View/Helper directory within this library
        $prefix = 'Wmp_View_Helper';
        $dir = dirname(__FILE__) . '/../../View/Helper'; //repertoir relatif : Wmp/View/Helper     
        $view->addHelperPath($dir, $prefix);
		
		// ajout des helpers spécifique au module en cours
		//Permet de vérifier la présence d'une view_helper en priorité dans le module
		//Ensuite dans Wmp_View, et Zend_View
		$prefix = $view->module.'_View_Helper';
        $dir =$config->path->modules->dir. '/'.$view->module.'/views/helpers';   
		$view->addHelperPath($dir, $prefix);
		/*---------------------------------------------------------------------------*/

		/*************Activation de ajax, et dojo************************************/
		//$view->addHelperPath("ZendX/JQuery/View/Helper", "ZendX_JQuery_View_Helper");
		$view->addHelperPath('Zend/Dojo/View/Helper/', 'Zend_Dojo_View_Helper');
		/************************Fin activation de ajax, et dojo**********************/
		
        // setup initial head wmp holders, metadata
        $view->headMeta()->appendHttpEquiv('Content-Type', 'text/html;charset='.zend_registry::get('config')->charset);
        
        if($actionParams->metakeywords)
        	$view->headMeta()->appendName('keywords', $view->WmpTranslate($actionParams->metakeywords));
        else if($moduleTable->metakeywords)
        	$view->headMeta()->appendName('keywords', $view->WmpTranslate($moduleTable->metakeywords));
        else
        	$view->headMeta()->appendName('keywords', $view->WmpTranslate($config->meta->keywords));
        	
        if($actionParams->metadescription)
        	$view->headMeta()->appendName('description', $view->WmpTranslate($actionParams->metadescription));
        else if($moduleTable->metadescription)
        	$view->headMeta()->appendName('description', $view->WmpTranslate($moduleTable->metadescription));
        else
        	$view->headMeta()->appendName('description', $config->meta->description);
    }
    
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        $this->_controllerCssLink($request);
    }
    
    public function postDispatch(Zend_Controller_Request_Abstract $request)
    {
        if (!$request->isDispatched()) {
            return;
        }
        $view = $this->_view;
        
		/*
		 * S'il n'y a pas de headTittle dans l'action du controlleur
		 * alors, on affecte la valeur title au headTitle
		 */
        if (count($view->headTitle()->getValue()) == 0) {
            $view->headTitle($view->title);
        }
        
        //ajoute -Wmp dans le headTitle
        $view->headTitle()->setSeparator(' - ');		
        $view->headTitle(zend_registry::get('config')->site->title);  

        //Log action in databases
		$view->WmpLog();
    }
    
    protected function _controllerCssLink(Zend_Controller_Request_Http $request)
    {
        $config = Zend_Registry::get('config');
    	$view = $this->_view;
        $action = zend_registry::get('action');
    	$controller = zend_registry::get('controller');
        $module = zend_registry::get('module');		
        $ModuleTemplate = zend_registry::get('template');          

  
        $template_path = '/templates/'.$ModuleTemplate.'/css';
        $local_path = $template_path.'/'.$request->getParam('language');
        $module_path = $local_path . '/' . $module;        
		
		$cssList = array(
				        'site_css'=>'/css/site',
						'template_css' => $template_path.'/template',		
						'local_css' => $local_path .'/local',
						'module_css' =>  $module_path.'/module',
						'module_controller_css' => $module_path.'/controller.'.$controller
					);
		$this->_addCss( $cssList );
		$this->_addCss( $cssList, "print" );		
    }
    
	private function _addCss($cssArray,$type="screen,projection")
    {
    	$config = zend_registry::get('config');
    	$extension =".css";
    	
    	if($type=="print") $extension=".print.css";  	
    	
    	foreach($cssArray as $key=>$val){
	    	$cssfile = $val.$extension;    		
	    	if (file_exists($config->path->public->dir.$cssfile)) {
	    		
	            $url = $this->_view->baseUrl().$cssfile;        
	            $this->_view->headLink()->appendStylesheet($url , $type);
	       	}
    	}    	
    }
}